#include "BLDCDriver.h"
#include "foc_utils.h"
void BLDCDriver_Init(BLDCDriver_t *handle)
{
	handle->initialized= true;
	if(!handle->voltage_limit)
	{
		handle->voltage_limit=handle->voltage_power_supply;
	}
}
void BLDC_SetPWM(BLDCDriver_t *handle,float Ua, float Ub, float Uc)
{
	  // limit the voltage in driver
  Ua = _constrain(Ua, 0, handle->voltage_limit);
  Ub = _constrain(Ub, 0, handle->voltage_limit);
  Uc = _constrain(Uc, 0, handle->voltage_limit);
  // calculate duty cycle
  // limited in [0,1]
  handle->phase.dc_a = _constrain(Ua / handle->voltage_power_supply, 0.0f , 1.0f );
  handle->phase.dc_b = _constrain(Ub / handle->voltage_power_supply, 0.0f , 1.0f );
  handle->phase.dc_c = _constrain(Uc / handle->voltage_power_supply, 0.0f , 1.0f );
	handle->function.setPwm(handle->phase.dc_a,handle->phase.dc_b,handle->phase.dc_c);
}
void BLDC_SetPhaseState(BLDCDriver_t *handle,PhaseState a,PhaseState b,PhaseState c)
{
	handle->function.setPhaseState(a,b,c);
}
void BLDC_disable(BLDCDriver_t *handle)
{
	handle->function.disable();
}
void BLDC_enable(BLDCDriver_t *handle)
{
	handle->function.disable();
}